﻿using System.Windows.Forms;
using System;
using System.IO;
using System.Diagnostics;

namespace JSMixer
{
    public partial class Form1 : Form
    {
        
        private string _selectedFolderPath = string.Empty;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Control && e.Alt && e.KeyCode == Keys.K)
            {
                new Eggs.EEggs().ShowDialog();
            }
        }

        private void toolStripButtonOpenFolder_Click(object sender, System.EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            fbd.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                _selectedFolderPath = fbd.SelectedPath;
                Infolabel.Text = string.Format("Selected folder : {0}", _selectedFolderPath);
                PopulateFileList(_selectedFolderPath);
            }
        }

        private void PopulateFileList(string selectedFolderPath)
        {
            checkedListBox1.Items.Clear();
            string[] files = Directory.GetFiles(selectedFolderPath, "*.js", IncludeFilesInSubDirectories.Checked?SearchOption.AllDirectories:SearchOption.TopDirectoryOnly);
            foreach (string file in files)
            {
                checkedListBox1.Items.Add(new CheckItem(file), true);
            }
            toolStripStatusLabel1.Text = string.Format("There are {0} files.", files.Length);
        }

        private void toolStripButtonCombine_Click(object sender, System.EventArgs e)
        {
            Combiner cb = new Combiner();
            cb.IncludeHeader = toolStripMenuItemAddFileHeader.Checked;

            foreach (CheckItem item in checkedListBox1.CheckedItems)
            {
                cb.Files.Add(item);
            }
            ResultRichTextBox.ResetText();
            ResultRichTextBox.AppendText(cb.GetCombination());
            ShowTextInfos();
        }

        private void toolStripButtonMinimize_Click(object sender, System.EventArgs e)
        {
            Compressor compressor = new Compressor();
            foreach (CheckItem item in checkedListBox1.CheckedItems)
            {
                compressor.Files.Add(item);
            }
            ResultRichTextBox.ResetText();
            ResultRichTextBox.AppendText(compressor.GetMergedAndCompressed());
            ShowTextInfos();
        }

        private void toolStripButtonSaveAs_Click(object sender, System.EventArgs e)
        {
            if (ResultRichTextBox.TextLength == 0)
            {
                MessageBox.Show("There is nothing to save", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "Javascript files(.js)|*.js";
            sfd.InitialDirectory = _selectedFolderPath;
            if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                ResultRichTextBox.SaveFile(sfd.FileName);
            }
        }
        private void ShowTextInfos()
        {
            string[] words = ResultRichTextBox.Text.Split(' ');
            toolStripStatusLabel1.Text = string.Format("{0} lines, {1} words, {2} characters", ResultRichTextBox.Lines.Length, words.Length, ResultRichTextBox.Text.Length);
        }

        private void CheckAll_Click(object sender, EventArgs e)
        {
            SetItemsChecked(true);
        }
        private void UncheckAll_Click(object sender, EventArgs e)
        {
            SetItemsChecked(false);
        }
        private void SetItemsChecked(bool isChecked)
        {
            for (int i = 0; i < checkedListBox1.Items.Count; i++)
            {
                checkedListBox1.SetItemChecked(i, isChecked);
            }
        }

        private void toolStripLabel2_Click(object sender, EventArgs e)
        {
            Process.Start("http://cssmixer.codeplex.com/");
        }

        private void toolStripLabel1_Click(object sender, EventArgs e)
        {
            Process.Start("http://www.sambeauvois.be/blog/");
        }
    }
}
